/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tank.models;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author shengchang
 */
public class Bullet implements Runnable{
    private int x;
    private int y;
    private int direction;
    private int speed = 5;
    private boolean isLive;
    DirectionInfo directionInfo;

    public boolean getIsLive() {
        return isLive;
    }
    
    public Bullet(int x, int y, DirectionInfo directionInfo)
    {
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.isLive = false;
        this.directionInfo = directionInfo;
    }
    
    
    /**
     *
     * @return
     */
    public int getX()
    {
        return this.x;
    }
    
    /**
     *
     * @return
     */
    public int getY()
    {
        return this.y;
    }
    
    /**
     *
     * @param x
     */
    public void setX(int x)
    {
        this.x = x;
    }
    
    /**
     *
     * @param y
     */
    public void setY(int y)
    {
        this.y = y;
    }

    @Override
    public void run() {
        int xMoveDistance = 0;
        int yMoveDistance = 0;
        while( true )
        {
            this.isLive = true;
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(Bullet.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            if(this.directionInfo.getSlope() >= 1 )
            {
                yMoveDistance = Math.abs(speed + y - Global.myTank.getY() - 25);
                xMoveDistance = (int)((double)yMoveDistance / this.directionInfo.getSlope());
                this.x = Global.myTank.getX() + 25 + xMoveDistance * this.directionInfo.getDirectionX();
                this.y = speed * this.directionInfo.getDirectionY() + y;
            }
            else if(this.directionInfo.getSlope() < 1 && this.directionInfo.getSlope() >= 0)
            {
                xMoveDistance = Math.abs(speed + x - Global.myTank.getX() - 25);
                yMoveDistance = (int)((double)xMoveDistance * this.directionInfo.getSlope());
                this.x = speed * this.directionInfo.getDirectionX() + x;
                this.y = Global.myTank.getY() + 25 + yMoveDistance * this.directionInfo.getDirectionY();
            }
            else
            {
                xMoveDistance = speed + x;
                yMoveDistance = 0;       
                this.y = speed * this.directionInfo.getDirectionY() + y;
                this.x = x;
            }
            
               
            /*if bullet to outside of the window, terminate this thread*/
            if( x > 800 || x < 0 || y > 600 || y < 0 )
            {
                this.isLive = false;
                break;
            }
        }
    }
}
